﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Transactions;
using DAL;
using System.Data.Objects;
using System.Data.Common;
namespace DAL
{
  public  class SessionRepository
    {
        //Get Primary Key for the Current Table
      public static string Getpk(string brnchid, string orgid) {
          SMSEntities Sms = new SMSEntities();
          var generated = Sms.GetPrimaryKey("Session").ToList();
          return String.Format("SA{0}.{1}-{2:00000000000000}", orgid, brnchid, Convert.ToInt32(generated[0].number));
      }
      //Start the Session 
        public static bool StartSession(string SessionName, DateTime StartDate, DateTime EndDate, string OrgID, string BranchID) {

            using (TransactionScope transaction = new TransactionScope()) {
                bool IsCreated = false;
                SMSEntities Sms = new SMSEntities();
                Session ISession = new Session();
                ISession.Active = true;
                ISession.CDate = System.DateTime.Now;
                ISession.EndDate = EndDate;
                ISession.StartDate = StartDate;
                ISession.SessionName = SessionName;
                ISession.BranchID = BranchID;
                ISession.OrgID = OrgID;
                ISession.SessionID = Getpk(OrgID, BranchID);
                Sms.Sessions.Add(ISession);
                Sms.SaveChanges();
                transaction.Complete();
                IsCreated = true;
                return IsCreated;
            }

        }
        //Overload OF StartSession
        public static bool StartSession(string SessionID, string SessionName, DateTime StartDate, DateTime EndDate, string OrgID, string BranchID) {

            using (TransactionScope transaction = new TransactionScope()) {
                bool IsCreated = false;
                SMSEntities Sms = new SMSEntities();
                Session ISession = new Session();
                ISession.Active = true;
                ISession.CDate = System.DateTime.Now;
                ISession.EndDate = EndDate;
                ISession.StartDate = StartDate;
                ISession.SessionName = SessionName;
                ISession.BranchID = BranchID;
                ISession.OrgID = OrgID;
                ISession.SessionID = SessionID;
                Sms.Sessions.Add(ISession);
                Sms.SaveChanges();
                transaction.Complete();
                IsCreated = true;
                return IsCreated;
            }

        }
        public static List< GetAllSessionActive_Result1> GetAllSessionActive(string OrgID, string BranchID) {

            SMSEntities Sms = new SMSEntities();
            return Sms.GetAllSession(OrgID, BranchID).ToList<GetAllSessionActive_Result1>();
            }
        }
    }

